{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-18T11:37:55.230469Z",
     "start_time": "2025-09-18T11:37:54.470428Z"
    }
   },
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import requests\n",
    "\n",
    "# 选择要获取的数据时间段\n",
    "periods = '3600'\n",
    "\n",
    "# 通过Http抓取btc历史价格数据\n",
    "resp = requests.get('https://api.cryptowat.ch/markets/gemini/btcusd/ohlc',\n",
    "  params={\n",
    "    'periods': periods\n",
    "  })\n",
    "data = resp.json()\n",
    "\n",
    "# 转换成pandas data frame\n",
    "df = pd.DataFrame(\n",
    "  data['result'][periods],\n",
    "  columns=[\n",
    "    'CloseTime',\n",
    "    'OpenPrice',\n",
    "    'HighPrice',\n",
    "    'LowPrice',\n",
    "    'ClosePrice',\n",
    "    'Volume',\n",
    "    'NA'])\n",
    "\n",
    "# 输出DataFrame的头部几行\n",
    "print(df.head())\n",
    "\n",
    "# 绘制btc价格曲线\n",
    "df['ClosePrice'].plot(figsize=(14, 7))\n",
    "plt.show()"
   ],
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'result'",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mKeyError\u001B[39m                                  Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[4]\u001B[39m\u001B[32m, line 17\u001B[39m\n\u001B[32m     13\u001B[39m data = resp.json()\n\u001B[32m     15\u001B[39m \u001B[38;5;66;03m# 转换成pandas data frame\u001B[39;00m\n\u001B[32m     16\u001B[39m df = pd.DataFrame(\n\u001B[32m---> \u001B[39m\u001B[32m17\u001B[39m   \u001B[43mdata\u001B[49m\u001B[43m[\u001B[49m\u001B[33;43m'\u001B[39;49m\u001B[33;43mresult\u001B[39;49m\u001B[33;43m'\u001B[39;49m\u001B[43m]\u001B[49m[periods],\n\u001B[32m     18\u001B[39m   columns=[\n\u001B[32m     19\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mCloseTime\u001B[39m\u001B[33m'\u001B[39m,\n\u001B[32m     20\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mOpenPrice\u001B[39m\u001B[33m'\u001B[39m,\n\u001B[32m     21\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mHighPrice\u001B[39m\u001B[33m'\u001B[39m,\n\u001B[32m     22\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mLowPrice\u001B[39m\u001B[33m'\u001B[39m,\n\u001B[32m     23\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mClosePrice\u001B[39m\u001B[33m'\u001B[39m,\n\u001B[32m     24\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mVolume\u001B[39m\u001B[33m'\u001B[39m,\n\u001B[32m     25\u001B[39m     \u001B[33m'\u001B[39m\u001B[33mNA\u001B[39m\u001B[33m'\u001B[39m])\n\u001B[32m     27\u001B[39m \u001B[38;5;66;03m# 输出DataFrame的头部几行\u001B[39;00m\n\u001B[32m     28\u001B[39m \u001B[38;5;28mprint\u001B[39m(df.head())\n",
      "\u001B[31mKeyError\u001B[39m: 'result'"
     ]
    }
   ],
   "execution_count": 4
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
